package cn.com.lyj6851.modules.edu.controller;


import cn.com.lyj6851.api.ApiResult;
import cn.com.lyj6851.api.MyShopException;
import cn.com.lyj6851.common.aop.NoRepeatSubmit;
import cn.com.lyj6851.common.bean.LocalUser;
import cn.com.lyj6851.common.interceptor.AuthCheck;
import cn.com.lyj6851.logging.aop.AppLog;
import cn.com.lyj6851.modules.edu.service.EduCourseService;
import cn.com.lyj6851.modules.edu.vo.CourseInfoVo;
import cn.com.lyj6851.modules.product.param.YdyStoreProductRelationQueryParam;
import cn.com.lyj6851.modules.product.service.YdyStoreProductRelationService;
import cn.hutool.core.util.NumberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * APP课程数据
 * @author Administrator
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "首页初始化", tags = "网校:首页初始化")
public class EduCourseController {
    private final EduCourseService eduCourseService;
    private final YdyStoreProductRelationService productRelationService;

    @GetMapping("course/detail")
    public ApiResult<CourseInfoVo> getCourseInfoVo(Long id){

        // 1.课程浏览量加1
        eduCourseService.incBrowseNum(id);

        CourseInfoVo courseInfoVo = eduCourseService.courseDetail(id);

        return ApiResult.ok(courseInfoVo);
    }

    /**
     * 添加收藏
     */
    @AppLog(value = "课程收藏", type = 1)
    @NoRepeatSubmit
    @AuthCheck
    @PostMapping("course/favorite")
    @ApiOperation(value = "课程收藏",notes = "课程收藏")
    public ApiResult<Boolean> collectAdd(@Validated @RequestBody YdyStoreProductRelationQueryParam param){
        long uid = LocalUser.getUser().getUid();
        if(!NumberUtil.isNumber(param.getId())) {
            throw new MyShopException("参数非法");
        }
        // 根据用户id及课程id添加收藏
        productRelationService.addRroductRelation(Long.parseLong(param.getId()),uid);
        return ApiResult.ok(true);
    }

}
